<?php
class Shop_Model_Manufacturer extends Promotor_Model_Scaffold {
	
	const UPLOAD_PATH = 'upload/shop/manufacturer/';

	protected $_dbTableClass = 'Shop_Model_DbTable_Manufacturer';
	
	protected $_cachedMethods = array(
		'findById',
		'findByAlias'
	);
	
	/**
	 * @param integer $Id
	 * @param bool $asArray
	 * @return array|Zend_Db_Table_Row_Abstract
	 */
	public function findById($Id, $asArray = true) {
		$table = $this->getDbTable();
		
		$select = $table->select()
						->where('id = ?', $Id)
						->where('publicated = 1');

		try {
			$result = $table->fetchRow($select);
			if (null !== $result) {
				$this->_setStatus(self::SUCCESS);
				return $asArray 
					? $result->toArray()
					: $result;
			} else {
				$this->_setStatus(self::FAILURE);
			}
		} catch (Zend_Db_Exception $e) {
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}
	}
	
	/**
	 * @param string $alias
	 * @param bool $asArray
	 * @return array|Zend_Db_Table_Row_Abstract
	 */
	public function findByAlias($alias, $asArray = true) {
		$table = $this->getDbTable();
		$where = $table->getAdapter()
							->quoteInto('alias = ? AND publicated = 1', $alias);

		try {
			$row = $table->fetchRow($where);
			if(!$row instanceof Zend_Db_Table_Row_Abstract) {
				$this->_setStatus(self::FAILURE);
				return;
			}
			$this->_setStatus(self::SUCCESS);
			return $asArray
				? $row->toArray()
				: $row;
		} catch (Zend_Db_Table_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}
	}
}